package com.qfjy.project.meeting;

import com.qfjy.common.utils.result.ResultCode;
import com.qfjy.common.utils.result.ResultJson;
import com.qfjy.entity.po.User;
import com.qfjy.project.weixin.api.messageCustom.MessageCustomUtil;
import com.qfjy.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/12/5
 * @Version 1.0
 */
@Controller
@RequestMapping("user")
@Slf4j
@Api(value="微信用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**微信 API 客服接口（微信主动给用户发消息）*/
    @Autowired
    private MessageCustomUtil messageCustomUtil;
    /**
     * 微信用户 登录（绑定）
     * @return
     */
    @GetMapping("login")
    @ResponseBody
    public Object login(@RequestParam("email") final String email,
                        @RequestParam("wid") final int wid,
                        @RequestParam("openid")String openid){

        User user=userService.selectUserByEmail(email);
        if(user==null){
            // 前端提示：邮箱不存在，请联系管理员进行后台申请
            return new ResultJson<>(null, ResultCode.NOT_DATA);
        }else{

            if(user.getWid()!=null){
                // 邮箱已被人绑定,请联系管理员
                return new ResultJson<>(user,ResultCode.SUCCESS_IS_HAVE);
            }else{
                //进行绑定功能 根据email更新wid
               int num= userService.updateUserWidByEmail(wid,email);
                log.info("用户微信绑定成功{},{}",wid,email);
                //同时微信主动给用户发送消息接口。(稍候实现） RabbitMQ或多线程
                // openid   content
                StringBuffer sBuffer=new StringBuffer("尊敬用户，您好"+user.getName()+"，欢迎使用平台，您所在的用户组是");
                sBuffer.append(user.getRid()==1?"发单组":"抢单组");
                sBuffer.append("，可进行相关操作，祝愉快");
                messageCustomUtil.sendMessageText(openid,sBuffer.toString());

                return new ResultJson<>(num,ResultCode.SUCCESS);
            }

        }
    }

    /**
     * 微信  个人中心 --更新个人信息
     */
    @PostMapping("update")
    @ResponseBody
    public Object updateUser(User user){
        int num=0;
        try {
             num = userService.updateByPrimaryKeySelective(user);
            log.info("用户个人信息更新成功{}{}", num, user);
        }catch (Exception ex){
            log.error("用户更新出现异常{},{}",ex.getMessage(),user.toString());
        }
        return new ResultJson<>(num,ResultCode.SUCCESS);
    }

    /**
     * 进入登录页面
     * /user/loginToPage
     */
    @GetMapping("loginToPage")
    public String loginToPage(HttpServletRequest request){
        String wid=request.getParameter("wid");
        String openid=request.getParameter("openid");
        request.setAttribute("wid",wid);
        request.setAttribute("openid",openid);
        return "user/login";
    }
    /**
     * 进入抢单列表页面
     * /user/meetingGrabListToPage
     */
    @GetMapping("meetingGrabListToPage")
    public String meetingGrabListToPage(HttpServletRequest request){
        String uid=request.getParameter("uid");
        request.setAttribute("uid",uid);
        return "meetingGrab/list";
    }
    /**
     * 进入无权限页面
     * /user/unauth
     */
    @GetMapping("unauth")
    public String unauth(HttpServletRequest request){

        return "unauth";
    }
}
